package com.xudow.app.ui.task.user;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import com.activeshare.edu.ucenter.common.messages.Message;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 用户基本信息修改
 *
 * @author lukecheng
 */
public class ProfileEditTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(ProfileEditTask.class);

    //登陆地址
    private static final String PROFILE_UPDATE_URL = Config.BASE_URL + Config.PROFILE_UPDATE_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;
    public static final int ERROR_WITH_MESSAGE = 2;

    private Context context;
    private Handler handler;

    public ProfileEditTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;
        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;
        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("Message", msg);
            message.setData(data);
        }
        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        HashMap<String, String> params = parameters[0];

        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();

        //昵称
        if (null != params.get("nick_name")) {
            valueMap.add("nick_name", params.get("nick_name"));
        }

        //真实姓名
        if (null != params.get("name")) {
            valueMap.add("name", params.get("name"));
        }

        //地址
        if (null != params.get("address")) {
            valueMap.add("address", params.get("address"));
        }

        //生日
        if (null != params.get("birthday")) {
            valueMap.add("birthday", params.get("birthday"));
        }

        //性别
        if (null != params.get("gender")) {
            valueMap.add("gender", params.get("gender"));
        }
        //签名
        if (null != params.get("labelname")) {
            valueMap.add("labelname", params.get("labelname"));
        }
        XApplication application = (XApplication) context.getApplicationContext();

        RestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        try {
            Message result = restTemplate.postForObject(PROFILE_UPDATE_URL, valueMap, Message.class);

            if (result != null) {
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result.getErrorMessage());
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(null);
        }

        return null;
    }
}
